// Uber shader fragment program.
uniform vec4 agtk_ambientColor;
uniform vec4 agtk_diffuseColor;
uniform vec4 agtk_falloffColor;
uniform float agtk_specularIntensity;
uniform float agtk_specularPower;
uniform float agtk_glowIntensity;
uniform float agtk_falloffIntensity;
uniform float agtk_falloffPower;
uniform float agtk_falloffDetail;
uniform sampler2D agtk_diffuseMap;
uniform sampler2D agtk_normalMap;
uniform sampler2D agtk_specularMap;
uniform sampler2D agtk_glowMap;
uniform bool agtk_useDiffuseMap;
uniform bool agtk_useNormalMap;
uniform bool agtk_useSpecularMap;
uniform bool agtk_useGlowMap;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying float FalloffValue;
varying vec3 Normal;

void main( void )
{
   vec4 diffuseMapColor = vec4(1.0, 1.0, 1.0, 1.0);
   vec4 normalMapColor = vec4(Normal, 0.0);
   vec4 specularMapColor = vec4(0.0, 0.0, 0.0, 1.0);
   vec4 glowMapColor = vec4(0.0, 0.0, 0.0, 1.0);
   if (agtk_useDiffuseMap) {
      diffuseMapColor = texture2D(agtk_diffuseMap, Texcoord );
   }
   if (agtk_useNormalMap) {
      normalMapColor = texture2D(agtk_normalMap, Texcoord );
   }
   if (agtk_useSpecularMap) {
      specularMapColor = texture2D(agtk_specularMap, Texcoord );
   }
   if (agtk_useGlowMap) {
      glowMapColor = texture2D(agtk_glowMap, Texcoord );
   }
   
   vec4 ambientColor = agtk_ambientColor;
   vec4 diffuseColor = agtk_diffuseColor;
   vec4 falloffColor = agtk_falloffColor;
   
   vec3 fragLightDirection = normalize( LightDirection );
   vec3 fragNormal = normalize( ( normalMapColor.xyz * 2.0 ) - 1.0 );
   float fragNDotL = dot( fragNormal, fragLightDirection );
   
   float falloffDetail = agtk_falloffDetail*((1.0-abs(dot(fragNormal, vec3(0.0,0.0,1.0))))*2.0-0.8);
   float falloffCompile = agtk_falloffIntensity*(falloffDetail+FalloffValue);
   falloffCompile/=agtk_falloffPower;
   
   vec3 fragReflection = normalize( ( ( 2.0 * fragNormal ) * fragNDotL ) - fragLightDirection ); 
   vec3 fragViewDirection = normalize( ViewDirection );
   float fragRDotV = max( 0.0, dot( fragReflection, fragViewDirection ) );
   
   vec4  TotalAmbient = ambientColor * diffuseMapColor; 
   vec4  TotalDiffuse = diffuseColor * fragNDotL * diffuseMapColor; 
   vec4  TotalSpecular = agtk_specularIntensity * specularMapColor * ( pow( fragRDotV, agtk_specularPower ) );
   vec4  TotalGlow = glowMapColor*agtk_glowIntensity;
   vec4  TotalFalloff = falloffColor*falloffCompile;
     
   vec4  TotalColor = (TotalAmbient + TotalDiffuse + TotalSpecular + TotalGlow + TotalFalloff);
  
   gl_FragColor = TotalColor;
}
/*
void main(void) {
   gl_FragColor = texture2D(agtk_normalMap, Texcoord);
}
*/